package com.kgc.web.listener; /**
 * <h3>项目名称:demo03050</h3>
 * <p>本API描述:${description}</p>
 * <p>@author : AliyZhang</p>
 * <p>@date : 2021-03-08 13:14</p>
 **/

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;

/*@WebListener*/
public class OnLineListener implements  HttpSessionBindingListener {
    private String userName;
    public OnLineListener(String userName) {
        this.userName=userName;
    }

    public OnLineListener() {
    }

    @Override
    public void valueBound(HttpSessionBindingEvent event) {
        HttpSession session = event.getSession();
        ServletContext servletContext = session.getServletContext();
        Object total = servletContext.getAttribute("total");
        if(total==null){
            servletContext.setAttribute("total", 1);
        }else{
            servletContext.setAttribute("total", ((int)total)+1);
        }
        System.out.println("在线用户数+1");
    }

    @Override
    public void valueUnbound(HttpSessionBindingEvent event) {
        HttpSession session = event.getSession();
        ServletContext servletContext = session.getServletContext();
        Object total = servletContext.getAttribute("total");
        servletContext.setAttribute("total", ((int)total)-1);
        System.out.println("在线用户数-1");
    }
}
